/**
 * 
 */
package info.niwota.webapp;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.net.Uri;
import android.text.TextUtils;

/**
 * @author qiangli
 *
 */
public class WebUtils {

	private WebUtils() {
	}


	public static String encodeURIComponent(String s)  {
		try {
			return URLEncoder.encode(s, "UTF-8");
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return s;
	}
	
	/**
	 * Avoid file name too long exception by adding path sep /
	 * @param s
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String validPathname(String s, boolean encode) throws UnsupportedEncodingException {
		s = (encode ? encodeURIComponent(s) : s);
		StringBuilder sb = new StringBuilder(s);
		int len = sb.length();
		final int max = 128;
		for (int i = max; i < len; i++) {
			sb.insert(i, "/");
		}
		return sb.toString();
	}
	
	/**
	 * 
	 * @param uri
	 * @return
	 */
	public static boolean isLocal(Uri uri) {
		String peer = uri.getQueryParameter("peer");
		String jid = uri.getQueryParameter("jid");
		return (TextUtils.isEmpty(peer) || peer.equals(jid));
	}
}
